package com.yl.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Fangjian;
import com.yl.auth.entity.Louceng;
import com.yl.auth.mapper.FangjianMapper;
import com.yl.auth.mapper.LoucengMapper;
import com.yl.auth.service.LoucengService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ln
 * @since 2023-06-16
 */
@Service
public class LoucengServiceImpl extends ServiceImpl<LoucengMapper, Louceng> implements LoucengService {
    @Resource
    private LoucengMapper loucengMapper;
    @Resource
    private FangjianMapper fangjianMapper;
    @Override
    public IPage<Louceng> getPage(Page<Louceng> pagefarm) {
        return loucengMapper.getpage(pagefarm);
    }

    @Override
    public Louceng selectById(Long id) {
        Louceng louceng = loucengMapper.selectById(id);
        Integer count = 0;
        if(louceng.getId()!=null){
            QueryWrapper<Fangjian> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("floor_id", louceng.getId());
            List<Fangjian> fangjians = fangjianMapper.selectList(queryWrapper);
            for (Fangjian fangjian : fangjians) {
                if(fangjian.getFloorId()==louceng.getId()){
                    count++;
                }
            }
        }
        louceng.setCount(count);
        return louceng;
    }

    @Override
    public IPage<Louceng> select(Page<Louceng> pagefarm) {
        return loucengMapper.select(pagefarm);
    }
}
